<template>
	<view class="login">
		<view class="login-tel">
			<view class="tel-main">
				<view class="logo">
					<image class="logo-img" src="../../static/img/logo.png" mode=""></image>
				</view>
				<view class="tel" @click="goLoginTel">手机号注册</view>
				<loginOther></loginOther>
				<view class="login-go">
					<view class="">已有账号，去登录</view>
					<!-- <image src="../../static/img/down.PNG" mode=""></image> -->
				</view>
			</view>
		</view>
		<view class="login-tel">
			<view class="tel-main">
				<!-- 关闭按钮 -->
				 <view class="close close-center">
					<view class="login-go2">
						<!-- <image class="close-img" src="../../static/img/up.PNG" mode=""></image> -->
						<view class="toLogin">没有账号,去注册</view>
					</view>
				</view>
				<view class="login-from">
					<view class="login-user">
						<text class="user-name">账号：</text>
						<input type="text" v-model="userName" value="" placeholder="请输入手机号昵称" />
					</view>
					<view class="login-user">
						<text class="user-name">密码：</text>
						<input type="text"  v-model="userPwd" value="" placeholder="6-16位字符" />
					</view>
				</view>
				<view class="login-quick">
					<view>忘记密码</view>
					<view>免密登录</view>
				</view>
				<view @click="submit" class="tel">登录</view>
				<view class="reminder">温馨提示:你可以选择免密登录，更加方便</view>
				<loginOther></loginOther>
			</view>
		</view>	
		<!-- 滑块布局  没有实现 -->
		<!-- <swiper vertical="true" style="height: 100vh;">
			<swiper-item>
				<scroll-view scroll-y="true">
				</scroll-view>
			</swiper-item>
			<swiper-item>
				<scroll-view scroll-y="true" >
				</scroll-view>
			</swiper-item>
		</swiper> -->
	</view>
</template>

<script>
	import $http from '../../common/api/request.js'
	import loginOther from '../../components/login/login-other.vue'
	import {mapMutations} from 'vuex'
	export default {
		data() {
			return {
				//用户输入的内容
				userName:"",
				userPwd:"",
				//验证规则
				rules:{
					userName:{
						// 不能为空
						rule:/\S/,
						msg:"用户名或账号不能为空"
					},
					userPwd:{
						// 不能为空
						rule:/^[0-9a-zA-Z]{6,16}$/,
						msg:"密码应该为6-16位字符",
					}
				}
			}
		},
		components: {
			loginOther
		},
		methods: {
			...mapMutations(['login']),
			// 关闭当前页面,返回上个页面
			goBack() {
				uni.navigateBack({
					delta:1
				})
			},
			//点击登录
			submit(){
				// 若验证不通过，则直接return
				if(  !this.validate('userName')  ) return;
				if(  !this.validate("userPwd") )   return;
				
				uni.showLoading({
					title:"登录中..."
				})
				$http.request({
					url:"/login",
					method:"POST",
					data:{
						userName:this.userName,
						userPwd:this.userPwd
					}
				}).then((res)=>{
					//存储并保存用户信息
					// console.log(res.data);
					this.login(res.data);
					uni.showToast({
						title:res.msg,
						icon:"none"
					})
					// //关闭提示框，并返回我的页面
					uni.hideLoading();
					uni.navigateTo({
						url:'../index/index'
					})
				}).catch(()=>{
					uni.showToast({
						title:'请求失败',
						icon:'none'
					})
				})
				
			},
			//判断验证是否符合规则
			validate(key) {
				let bool = true;
				if(!this.rules[key].rule.test(this[key])) {
					uni.showToast({
						title:this.rules[key].msg,
						icon:"none"
					})
					bool=false;
					return false;
				}
				return bool;
			},
			//进入手机号注册页面
			goLoginTel() {
				uni.navigateTo({
					url:'../login-tel/login-tel'
				})
			}
		}
	}
</script>

<style scoped>
.login-tel {
	width: 100vw;
	height: 100vh;
}
.tel-main {
	padding: 0 20rpx;
}
.close {
	/* padding: 120rpx 0; */
}
.close-img {
	width: 60rpx;
	height: 60rpx;
}
.logo {
	padding-bottom: 100rpx;
	display: flex;
	justify-content: center;
}
.logo-img {
	
}
.tel {
	width: 100%;
	height: 80rpx;
	line-height: 80rpx;
	text-align: center;
	color: #FFFFFF;
	background-color: #49BDFB;
	border-radius: 40rpx;
}

//跳转样式
.login-go {
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
}
.login-go image {
	width: 60rpx;
	height: 60rpx;
}

/* 滑动页 第二页 */
.close-center {
	display: flex;
}
/* .close-center view{
	flex: 1;
} */
.login-go2 {
	margin-left: 200rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}
.login-from {
	padding-top: 100rpx;
}
.toLogin {
	display: flex;
	justify-content: center;
	align-items: center;
}
.login-user {
	font-size: 32rpx;
	padding: 20rpx 0;
	display: flex;
	align-items: center;
	border-bottom: 2rpx solid #CCCCCC;
}
.user-name {
	padding-right: 5rpx;
}
.login-quick {
	display: flex;
	justify-content: space-between;
	padding: 20rpx 0;
}
.reminder {
	color: #CCCCCC;
	font-size: 32rpx;
	padding: 20rpx 0;
	text-align: center;
}
</style>
